package com.ygu.user.dao;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ygu.user.service.adapter.UserAdapter;
import com.ygu.websocket.domain.entity.User;
import com.ygu.user.mapper.UserMapper;
import com.ygu.user.service.IUserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author <a href="https://github.com/zongzibinbin">abin</a>
 * @since 2023-10-12
 */
@Service
public class UserDao extends ServiceImpl<UserMapper, User> implements IUserService {

    @Resource
    private UserMapper userMapper;

    /**
     * 通过openId 查询用户
     * @param openId 用户的唯一id
     * @return 用户信息
     */
    public User getUserByOpenId(String openId) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<User>().eq(User::getOpenId, openId);
        return userMapper.selectOne(queryWrapper);
    }

    /**
     * 通过openId 创建用户
     * @param openId 用户唯一的id
     * @return 成功
     */
    public Boolean register(String openId) {
        User user = UserAdapter.buildUserSave(openId);
        int result = userMapper.insert(user);
        return result > 0;
    }

    public User getUserById(Long id) {
       return userMapper.selectById(id);
    }
}
